package com.proposition.web.logger;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.zxing.common.StringUtils;
import com.proposition.commons.model.SysUserLog;
import com.proposition.logger.service.SysUserLogService;
import com.proposition.support.utils.Rest;
import com.proposition.support.utils.SpringUtils;
import org.apache.poi.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * ${DESCRIPTION}
 *
 * @author 杨凯
 * @create 2017-11-13 9:18
 **/
@Controller
@RequestMapping("/logger")
public class LoggerController {

	public static final Logger logger = LoggerFactory.getLogger(LoggerController.class);

	@Autowired
	private SysUserLogService logService;

	@RequestMapping("/page")
	@RequiresRoles("admin")
	public String loggerPage(){
		return "logger/log-list";
	}

	@ResponseBody
	@RequiresRoles("admin")
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	public Map loggerList(@RequestParam(defaultValue = "0") int start, @RequestParam(defaultValue = "7") int length,
	                      @RequestParam String startTime,
	                      @RequestParam String endTime,
	                      @RequestParam(required = false) String username ) {
		try {
			Map<String,Object> map = new HashMap<>(3);
			Page page = new Page(start/length+1, length);
			EntityWrapper ew=new EntityWrapper();
			ew.setEntity(new SysUserLog());
			if(startTime !=null&&endTime!=null){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = sdf.parse(endTime);
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
				calendar.add(Calendar.DATE,1);
				date=calendar.getTime();
				endTime = sdf.format(date);
				ew.between("time",startTime,endTime);
			}
			ew.like("username",username);
			ew.orderBy("time",false);
			Page<SysUserLog> paperPage = logService.selectPage(page,ew);
			Integer count = logService.selectCount(ew);
			List<SysUserLog> list = paperPage.getRecords();
			map.put("data", list);
			map.put("iTotalRecords",count);
			map.put("iTotalDisplayRecords",count);
			logger.info(list.toString());
			return map;
		} catch (Exception e) {
			logger.error("获取日志列表异常[{}]",e.getMessage());
		}
		return null;
	}

	/**
	 * 清空日志
	 * @return
	 */
	@ResponseBody
	@GetMapping("/clear")
	public Rest clearLogger(){
		Rest.Builder rest = Rest.newBuilder();
		try {
			logService.delete(new EntityWrapper<SysUserLog>());
			return rest.status(1001).build();
		} catch (Exception e) {
			logger.error("日志清空异常[{}]",e.getMessage());
		}
		return rest.build();
	}
}
